package ch.fhnw.qualiscan.application.elements;

import java.util.ArrayList;
import java.util.Set;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.modals.AddBestPractice;
import ch.fhnw.qualiscan.application.modals.Chart;
import ch.fhnw.qualiscan.application.models.DomainModel;
import ch.fhnw.qualiscan.application.pages.Evaluation;
import ch.fhnw.qualiscan.core.db.pojos.BestPractice;
import ch.fhnw.qualiscan.core.db.pojos.Domain;
import ch.fhnw.qualiscan.core.db.pojos.DomainText;
import ch.fhnw.qualiscan.core.db.pojos.Question;
import ch.fhnw.qualiscan.core.helpers.Filter;
import ch.fhnw.qualiscan.core.helpers.SessionHelper;
import ch.fhnw.qualiscan.core.helpers.TextHelper;
import ch.fhnw.qualiscan.core.libraries.ModalLib;

import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.BaseTheme;

import eu.livotov.tpt.i18n.TM;

@SuppressWarnings("serial")
public class EvaluationDomain extends VerticalLayout {
	
	HorizontalLayout wrap;
	VerticalLayout contentWrap;
	VerticalLayout content;
	HorizontalLayout header;
	VerticalLayout slider;
	Button toggleButton;
	Button detailChartButton;
	
	Embedded flashChart;
	HorizontalLayout flashWrap;
	
	Domain domain;
	
	ModalLib modalLib;
	
	Evaluation evaluation;
	
	SessionHelper sessionHelper;
	
	TextHelper textHelper;
	
	Filter filter;
	
	ArrayList<EvaluationQuestion> allQuestions = new ArrayList<EvaluationQuestion>();
	
	boolean open = false;
	
	public EvaluationDomain(Evaluation evaluation, Domain d, Filter filter) {
		this.filter = filter;
		this.domain = d;
		this.evaluation = evaluation;
		sessionHelper = new SessionHelper();
		modalLib = new ModalLib();
		textHelper = new TextHelper();
		
		buildElement();
	}
	
	public void buildElement() {
		setWidth("100%");
		setMargin(false, false, true, false);
		setSpacing(true);
		setStyleName("domainItem");
		
		wrap = new HorizontalLayout();
		wrap.setWidth("100%");
		wrap.setSpacing(true);
		
		contentWrap = new VerticalLayout();
		contentWrap.setWidth("100%");
		
		slider = new VerticalLayout();
		slider.setWidth("30px");
		
		// open/close switcher
		toggleButton = new Button(null);
		toggleButton.setIcon(Config.ICON_DOWN);
		toggleButton.setStyleName(BaseTheme.BUTTON_LINK);
		toggleButton.setWidth("20px");
		toggleButton.setHeight("20px");
		toggleButton.addListener(new Button.ClickListener() {	
			public void buttonClick(ClickEvent event) {
				toggleDomain();
			}
		});
		slider.addComponent(toggleButton);
		
		buildHeader();
		contentWrap.addComponent(buildContent());
		
		wrap.addComponent(contentWrap);
		wrap.addComponent(slider);
		
		addComponent(wrap);
		
		wrap.setExpandRatio(contentWrap, 1.0f);
	}
	
	public void buildHeader() {
		header = new HorizontalLayout();
		header.setMargin(false, false, true, false);
		header.setSpacing(true);
		header.setWidth("100%");
		
		DomainModel domainModel = new DomainModel();
		DomainText domainText = domainModel.getDomainText(domain, sessionHelper.getUserSession().getLanguage());

		// title
		Label domainTitle = textHelper.h2(domainText.getText());
		domainTitle.setHeight("40px");
		
		// flash chart
		flashWrap = new HorizontalLayout();
		flashWrap.addComponent(generateFlash(filter));
		
		// detailed chart button
		detailChartButton = new Button(TM.get("evaluationdomain.detailchart"));
		detailChartButton.setIcon(Config.ICON_DETAILCHART);
		detailChartButton.setStyleName(BaseTheme.BUTTON_LINK);
		detailChartButton.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				String url = Config.BASE_URL + "api/charts/flash/?charttype=1%26language=2%26survey=" + filter.getSurveyId() + "%26domain="+domain.getId()+"%26question=%26role="+ filter.getRoleId() +"%26user="+sessionHelper.getUserSession().getId()+"%26sur="+filter.getSurId()+"";
				//System.out.println("Domain big url: "+url);
				modalLib.openModal(new Chart(url, 0, domain.getId()));
			}
		});
		
		if(filter.getRoleId() == 0) {
			detailChartButton.setVisible(true);
		} else {
			detailChartButton.setVisible(false);
		}
		
		// best practice
		BestPractice userBestPractice = domainModel.getBestPracticeOfDomain(evaluation.sur, domain);
		final Button bestPracticeButton;
		if (userBestPractice == null || (userBestPractice.getTitle().equals("") && userBestPractice.getBestpractice().equals(""))) {
			bestPracticeButton = new Button(TM.get("bestpractice.add"));
			bestPracticeButton.setIcon(Config.ICON_BESTPRACTICE);
		}
		else {
			bestPracticeButton = new Button(TM.get("bestpractice.edit"));
			bestPracticeButton.setIcon(Config.ICON_EDITBESTPRACTICE);
		}
		bestPracticeButton.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				modalLib.openModal(new AddBestPractice(evaluation.sur, domain, bestPracticeButton));
			}
		});
		
		// left box
		HorizontalLayout leftBox = new HorizontalLayout();
		leftBox.setMargin(true, true, true, false);
		leftBox.addComponent(domainTitle);
		leftBox.setWidth("100%");
		leftBox.addComponent(bestPracticeButton);
		
		// right box
		VerticalLayout rightBox = new VerticalLayout();
		rightBox.addComponent(flashWrap);
		rightBox.setSpacing(false);
		rightBox.setWidth("300px");
		rightBox.setMargin(true, false, true, false);
		rightBox.addComponent(detailChartButton);
		
		// slider
		slider.setComponentAlignment(toggleButton, Alignment.TOP_RIGHT);
		
		header.addComponent(leftBox);
		header.addComponent(rightBox);
		
		contentWrap.addComponent(header);
		
		header.setExpandRatio(leftBox, 1.0f);
	}
	
	public VerticalLayout buildContent() {
		content = new VerticalLayout();
		content.setVisible(false);
		
		// load questions
		Set<Question> questions = domain.getQuestions();
		
		int i = 0;
		for(Question question : questions) {
			i++;
			EvaluationQuestion newElement = new EvaluationQuestion(question, filter, i);
			content.addComponent(newElement);
			allQuestions.add(newElement);
		}
		
		return content;
	}
	
	public void update(Filter filter) {
		this.filter = filter;
		
		// update detail chart status
		if(filter.getRoleId() == 0) {
			detailChartButton.setVisible(true);
		} else {
			detailChartButton.setVisible(false);
		}
		
		// update all childs
		for(EvaluationQuestion question : allQuestions) {
			question.update(filter);
		}
		
		flashWrap.removeAllComponents();
		flashWrap.addComponent(generateFlash(filter));
		
	}
	
	public void toggleDomain() {
		if(open == false) {
			slider.setComponentAlignment(toggleButton, Alignment.BOTTOM_RIGHT);
			open = true;
			content.setVisible(true);
			toggleButton.setIcon(Config.ICON_UP);
		} else {
			slider.setComponentAlignment(toggleButton, Alignment.TOP_RIGHT);
			open = false;
			content.setVisible(false);
			toggleButton.setIcon(Config.ICON_DOWN);
		}
	}
	
	public Embedded generateFlash(Filter filter) {
		String url = ""+Config.BASE_URL+"api/charts/flash/?charttype=0%26language=2%26survey="+filter.getSurveyId() +"%26domain="+ domain.getId() + "%26question=%26role="+ filter.getRoleId() +"%26user="+sessionHelper.getUserSession().getId()+"%26sur="+filter.getSurId()+"";
		//System.out.println("Domain small url: "+url);
		flashChart = new Embedded(null, new ThemeResource("../qualiscan/charts/simpleChart.swf?jsondataurl=" + url));
		flashChart.setMimeType("application/x-shockwave-flash");
		flashChart.setParameter("wmode", "opaque");
		flashChart.setHeight("40px");
		flashChart.setWidth("300px");
		
		return flashChart;
	}
	
}
